package com.chixing.service.impl;

import com.chixing.pojo.Good;
import com.chixing.service.RedisLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * 点赞业务 service
 */
@Service
public class RedisLikeServiceImpl implements RedisLikeService {
  @Autowired
  private RedisTemplate redisTemplate;
  @Override
  public void saveLike2Redis(Good good, Integer praiseCount) {
    //获得用户id 和 评论id
    Integer customerId = good.getCustomerId();
    Integer commentId = good.getCommentId();

    //设置redis key值 用户id :: 评论id
    String key = customerId + "::" + commentId;

    //设置redis value值
    Integer value = good.getGoodStatus();

    //存储点赞信息到redis (10109::30001 1) save_praise_record 保存_点赞_记录
    redisTemplate.opsForHash().put("save_praise_record" ,key,value);

    //添加评论的点赞数
    String commentCountKey = commentId.toString();
    System.out.println("commentCountKey: " + commentCountKey);

    //如果key ：comment_praise_count 存在。
    if (redisTemplate.hasKey("comment_praise_count")) {

      //如果 commentCountKey 已在redis中存在。即该评论id已经在redis中存在。
      if (redisTemplate.opsForHash().hasKey("comment_praise_count",commentCountKey)) {
        //获得该评论的点赞数
        Integer praiseCountValue = (Integer)redisTemplate.opsForHash().get("comment_praise_count", commentCountKey);
        //点赞数 + 1
        praiseCountValue++;
        System.out.println("点赞数：" + praiseCountValue);

        redisTemplate.opsForHash().put("comment_praise_count",commentCountKey,praiseCountValue);

      } else {
        redisTemplate.opsForHash().put("comment_praise_count",commentCountKey,praiseCount);
      }
    } else {
      redisTemplate.opsForHash().put("comment_praise_count",commentCountKey,praiseCount);
    }
  }

  @Override
  public void cancleLike2Redis(Good good, Integer praiseCount) {

    //获得用户id，商品id
    Integer commentId = good.getCommentId();
    Integer customerId = good.getCustomerId();

    //设置redis key值
    String key = customerId + "::" + commentId;

    //设置redis value值
    Integer value = good.getGoodStatus();
    System.out.println("value --" + value);

    //存储点赞信息到redis (10109::30001 1) cancle_praise_record 保存_点赞_记录
    redisTemplate.opsForHash().put("cancle_praise_record" ,key,value);

    //添加评论的点赞数
    String commentCountKey = commentId.toString();

    if (redisTemplate.hasKey("comment_praise_count")) {
      if (redisTemplate.opsForHash().hasKey("comment_praise_count",commentCountKey)) {
        Integer praiseCountValue = (Integer)redisTemplate.opsForHash().get("comment_praise_count", commentCountKey);
        praiseCountValue--;
        System.out.println("praiseCountValue" + praiseCountValue);
        redisTemplate.opsForHash().put("comment_praise_count",commentCountKey,praiseCountValue);
      } else {
        redisTemplate.opsForHash().put("comment_praise_count",commentCountKey,praiseCount);
      }
    } else {
      redisTemplate.opsForHash().put("comment_praise_count",commentCountKey,praiseCount);
    }

  }
}
